﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;

namespace WebService
{
    public class Service1 : IService1
    {
        /// <summary>
        /// Retourne une liste de tout les produits
        /// </summary>
        /// <returns>categoryList</returns>
        public List<Category> GetCategoryList()
        {
            DataTable categoryDT = BU.CategoryManager.Fetch();
            List<Category> categoryList = new List<Category>();

            foreach (DataRow row in categoryDT.Rows)
            {
                Category item = new Category(
                    (int)row["categoryid"],
                    row["name"].ToString(),
                    row["description"].ToString()
                );
                categoryList.Add(item);
            }
            return categoryList;
        }

        /// <summary>
        /// Retourne une liste de tous les produits contenus dans la catégorie demandée
        /// </summary>
        /// <param name="categoryID">int categoryID</param>
        /// <returns>productList</returns>
        public List<Product> GetProductList(int categoryID)
        {
            DataTable productDT = BU.ProductManager.GetProductList(categoryID);
            List<Product> productList = new List<Product>();

            foreach(DataRow row in productDT.Rows)
            {
                Product item = new Product(
                    (int)row["productid"],
                    row["name"].ToString(),
                    (decimal)row["price"],
                    (int)row["stock"],
                    row["description"].ToString(),
                    (int)row["categoryid"]
                );
                productList.Add(item);
            }
            return productList;
        }

        /// <summary>
        /// Retourne une liste des produits en promotion
        /// </summary>
        /// <returns>List<Product> promoList</returns>
        public List<Product> GetPromoList()
        {
            DataTable promoDT = BU.ProductManager.GetPromoList();
            List<Product> promoList = new List<Product>();

            foreach (DataRow row in promoDT.Rows)
            {
                Product item = new Product(
                    (int)row["productid"],
                    row["name"].ToString(),
                    (decimal)row["price"],
                    (int)row["stock"],
                    row["description"].ToString(),
                    (int)row["categoryid"]
                );
                promoList.Add(item);
            }
            return promoList;
        }

        /// <summary>
        /// Retourne la catégorie correspondant à l'id demandé
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns>Category category</returns>
        public Category GetCategory(int categoryID)
        {
            DataRow row = BU.CategoryManager.GetCategory(categoryID);
            Category category = new Category(
                (int)row["categoryid"],
                row["name"].ToString(),
                row["description"].ToString()
            );
            return category;
        }

        /// <summary>
        /// Retourne le produit correspondant à l'id demandé
        /// </summary>
        /// <param name="productID">int productID</param>
        /// <returns>Product product</returns>
        public Product GetProduct(int productID)
        {
            DataRow row = BU.ProductManager.GetProduct(productID);
            Product product = new Product(
                (int)row["productid"],
                row["name"].ToString(),
                (decimal)row["price"],
                (int)row["stock"],
                row["description"].ToString(),
                (int)row["categoryid"]
            );
            return product;
        }
    }
}